# DATA PROCESSING --------------------------------------------------------------
# Note: Any factor level that does not meet the 5% threshold is merged with the  
# closest related level.

# Controls ---------------------------------------------------------------------

# Demographics
df$age_log <- log(df$age)
df$female <- factor(df$female, levels = c(0, 1), labels = c("No", "Yes"))
df$white <- factor(df$white, levels = c(0, 1), labels = c("No", "Yes"))
df$hispanic <- factor(df$hispanic, levels = c(0, 1), labels = c("No", "Yes"))
df$college <- factor(df$college, levels = c(0, 1), labels = c("No", "Yes"))
df$veteran <- factor(df$veteran, levels = c(0, 1), labels = c("No", "Yes"))
df$employed <- factor(df$employed, levels = c(0, 1), labels = c("No", "Yes"))
df$income <- factor(df$income, levels = c(0, 1, 2, 3, NA), 
                    labels = c("<25K", "25K-50K", "50K-100K", ">100K"))
df$income_imputed <- impute(df$income) 
df$married <- factor(df$married, levels = c(0, 1), labels = c("No", "Yes"))
df$order <- as.factor(df$order)

# News consumption
df$news_freq <- factor(df$news_freq, levels = c(0, 1, 2), 
                       labels = c("Rarely", "Regularly", "Frequently"))

# Political knowledge
df$know_scan1 <- factor(df$know_scan1, levels = c(0, 1), 
                        labels = c("Incorrect", "Correct"))

df$know_scan2 <- factor(df$know_scan2, levels = c(0, 1), 
                        labels = c("Incorrect", "Correct"))

df$know_stable1 <- factor(df$know_stable1, levels = c(0, 1), 
                          labels = c("Incorrect", "Correct"))

df$know_stable2 <- factor(df$know_stable2, levels = c(0, 1), 
                          labels = c("Incorrect", "Correct"))

df$know_high <- ifelse(df$know_total >= 2, 1, 0) |>
  factor(levels = c(0, 1), labels = c("Low", "High"))

df$know_total <- as.factor(df$know_total)

# Political Trust
df$trust_gov <- ifelse(df$trust_gov == 4, 3, df$trust_gov)  |>
  factor(levels = c(0, 1, 2, 3), labels = c("Never", 
                                            "Some of the time", 
                                            "About half of the time", 
                                            "Most of the time")) |>
  relevel(ref = "Never")
  
# Foreign Policy Orientations --------------------------------------------------

# Cooperative internationalism
df$ci_imputed <- impute(df$ci, mean)
df$ci_high <- set_dummy(df$ci_imputed)

# Militant internationalism
df$mi_imputed <- impute(df$mi, mean)
df$mi_high <- set_dummy(df$mi_imputed)

# Isolationism
df$iso_imputed <- impute(df$iso, mean)
df$iso_high <- set_dummy(df$iso_imputed)

# Partisan identity ------------------------------------------------------------

# PID3
df$party <- factor(df$party , levels = c(0, 1, 2), 
                   labels = c("Democrat", "Independent", "Republican")) |>
  relevel(ref = "Independent")

# PID7
df$party_lean <- factor(df$party_lean, levels = c(0, 1, 2, 3, 4, 5, 6), 
                        labels = c("Strong Democrat", "Weak Democrat", 
                                   "Lean Democrat", "Independent",
                                   "Lean Republican","Weak Republican",
                                   "Strong Republican")) |>
  relevel(ref = "Independent")

# Attention Check --------------------------------------------------------------
df$attn_pre <- factor(df$attn_pre, levels = c(0, 1), labels = c("Fail", "Pass"))

df$attn_post1 <- if(experiment == "1") {
  factor(df$attn_post1, levels = c(0, 1), labels = c("Fail", "Pass"))
} else {
  factor(df$attn_post1, levels = c(0, 1), labels = c("Fail", "Pass"))
}

df$attn_post2 <- if(experiment == "1") {
  factor(df$attn_post2, levels = c(0, 1), labels = c("Fail", "Pass"))
} else {
  factor(df$attn_post2, levels = c(0, 1), labels = c("Fail", "Pass"))
}

df$attn_check <- ifelse(df$attn_total == 3, 1, 0) |>
  factor(levels = c(0, 1), labels = c("Fail", "Pass")) 

df$attn_check_post <- ifelse(df$attn_post1 == "Pass" & df$attn_post2 == "Pass", 
                             1, 0) |>
  factor(levels = c(0,1), labels = c("Fail", "Pass"))

df$attn_total <- as.factor(df$attn_total)

# Treatments -------------------------------------------------------------------
if(experiment == "1") {
  source("code/var_1.R")
} else if(experiment == "2") {
  source("code/var_2.R")
} else if(experiment == "3") {
  source("code/var_3.R")
}